<div id="main">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="ntModalLabel">{:lang('designated_function_assigned_to')}</h4>
    </div>
    <div class="modal-body">
        <div class="row clearfix">
            <div class="col-md-12 column">
                <div class="alert alert-dismissable alert-info">
                    <button type="button" class="close">×</button>
                    <p></p>
                </div>
            </div>
        </div>
        <div class="row clearfix">
            <div class="col-md-12">
                <table class="table table-striped" v-cloak>
                    <tbody>
                    <tr>
                        <td>{:lang('subject_name')}</td>
                        <td> {{name}}</td>
                    </tr>
                    </tbody>
                </table>
                <input type="hidden" id="did" value="{$id|default=''}">
            </div>
        </div>
        {eq name="$Think.session.admin.position_id" value="1"}
        <!--管理员出现三个国家-->
        <div class="col-md-12 column">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#allcountry" data-toggle="tab" @click="changecountry('0')">{:lang('global')}</a>
                </li>
                <li>
                    <a href="#zhcountry" data-toggle="tab" @click="changecountry('2')">{:lang('CHINA')}</a>
                </li>
                <li>
                    <a href="#bacountry" data-toggle="tab" @click="changecountry('1')">{:lang('BRAZIL')}</a>
                </li>
            </ul>
        </div>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane active" id="allcountry">
                <div class="row clearfix">
                    <div class="col-md-12">
                        <div class="cat_panel">
                            <table>
                                <thead>
                                <tr>
                                    <td colspan="2">{:lang('job_authorized')}</td>
                                </tr>
                                </thead>
                            </table>
                            <div style="max-height: 40vh;overflow: auto;">
                                <table v-cloak>
                                    <tbody>
                                    <tr v-for="(item,index) in positions">
                                        <td><label style="font-weight: normal;width: 100%;"><input class="cheitem"
                                                                                                   style="margin-right: 100px;"
                                                                                                   type="checkbox"
                                                                                                   v-model="checkall"
                                                                                                   :value="item.id">
                                            <template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;
                                            </template>
                                            {{item.langname}}</label>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="zhcountry">
                <div class="row clearfix">
                    <div class="col-md-12">
                        <div class="cat_panel">
                            <table>
                                <thead>
                                <tr>
                                    <td colspan="2">{:lang('job_authorized')}</td>
                                </tr>
                                </thead>
                            </table>
                            <div style="max-height: 40vh;overflow: auto;">
                                <table v-cloak>
                                    <tbody>
                                    <tr v-for="(item,index) in positions">
                                        <td><label style="font-weight: normal;width: 100%;"><input class="cheitem"
                                                                                                   style="margin-right: 100px;"
                                                                                                   type="checkbox"
                                                                                                   v-model="checkall"
                                                                                                   :value="item.id">
                                            <template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;
                                            </template>
                                            {{item.langname}}</label>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="bacountry">
                <div class="row clearfix">
                    <div class="col-md-12">
                        <div class="cat_panel">
                            <table>
                                <thead>
                                <tr>
                                    <td colspan="2">{:lang('job_authorized')}</td>
                                </tr>
                                </thead>
                            </table>
                            <div style="max-height: 40vh;overflow: auto;">
                                <table v-cloak>
                                    <tbody>
                                    <tr v-for="(item,index) in positions">
                                        <td><label style="font-weight: normal;width: 100%;"><input class="cheitem"
                                                                                                   style="margin-right: 100px;"
                                                                                                   type="checkbox"
                                                                                                   v-model="checkall"
                                                                                                   :value="item.id">
                                            <template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;
                                            </template>
                                            {{item.langname}}</label>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {/eq}
        {neq name="$Think.session.admin.position_id" value="1"}
        <!--不是全局管理员,只展示自己部门下的职位-->
        <div class="row clearfix">
            <div class="col-md-12">
                <div class="cat_panel">
                    <table>
                        <thead>
                        <tr>
                            <td colspan="2">{:lang('job_authorized')}</td>
                        </tr>
                        </thead>
                    </table>
                    <div style="max-height: 40vh;overflow: auto;">
                        <table v-cloak>
                            <tbody>
                            <tr v-for="(item,index) in positions">
                                <td><label style="font-weight: normal;width: 100%;"><input class="cheitem"
                                                                                           style="margin-right: 100px;"
                                                                                           type="checkbox" v-model="checkall" :value="item.id"><template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;</template>{{item.langname}}</label>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        {/neq}
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal"><span
                class="glyphicon glyphicon-remove"></span>{:lang('cancel')}
        </button>
        <button type="button" @click="savefun" class="btn btn-primary"><span
                class="glyphicon glyphicon-floppy-disk"></span>{:lang('save')}
        </button>
    </div>
</div>
<script>
    new Vue({
        el: '#main',
        data: {
            name: '',
            positions: [],//职位
            checkall: [],
            repeat: true,
            country_id: '0',
            lang: 'zh',
        },
        methods: {
            savefun(){
                if (this.repeat) {
                    $.ajax({
                        url: '/admin/finance/setfinaccroot',
                        type: 'post',
                        data: {
                            id: $('#did').val(),
                            position_ids: this.checkall,
                            act: 'setaccroot'
                        },
                        success: (data) => {
                            data = JSON.parse(data)
                            
                            if (data.status != 1) {
                                change_mes_type('.modal-body .alert',data.status)
                                $('.modal-body .alert').show()
                                $('.modal-body .alert p').html('*' + data.msg + '<br>')
                            } else {
                                change_mes_type('.alert',data.status)
                                this.repeat = false
                                $('.alert').show()
                                $('.alert p').html(data.msg + '<br>')
                                $('#position_setModal').modal('hide')
                            }
                        }
                    })
                }
            },
            //改变国家获取对应职位
            changecountry(country_id){
                this.country_id = country_id
                this.getdata();
            },
            getdata(){
                $.ajax({
                    url: '/admin/api/postionfrmaes',
                    data: {
                        country_id: this.country_id,
                        table: 'position',
                        lang: this.lang
                    },
                    type: 'post',
                    dataType: 'json',
                    success: (data) => {
                        this.positions = data;
                    }
                })
            },
            getalldata(){
                $.ajax({
                    url: '/admin/finance/setfinaccroot',
                    data: {
                        getroot: '',
                        id: $('#did').val()
                    },
                    type: 'post',
                    success: (data) => {
                        data = JSON.parse(data);
                        if (!data.country_id) {
                            //全局理员，授权职位默认展示
                            this.country_id = '0';
                        } else {
                            //非全局管理员 默认展示
                            this.country_id = data.country_id;
                        }
                        this.getdata();//展示国家下的职务
                        this.name = data.name;
                        this.lang = data.lang;
                        //授权职位
                        this.checkall = data.info.position_ids.split(',')
                    }
                })
                $(document).keydown((event) => {
                    if (event.keyCode == 13) {
                        this.savefun()
                    }
                });
            }
        },
        mounted(){
            this.getalldata()
        }
    })
</script>
